package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	public static final long ONE_DAY_MILLIS = 24 * 3600 * 1000L;
	
	public static String formatDate(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
		return outFormat.format(date);
	}
	public static String formatDate(Date date,String fromat) {
		if (date == null)
			return "";
		SimpleDateFormat outFormat = new SimpleDateFormat(fromat);
		return outFormat.format(date);
	}
	public static String formatDateTime(Date date) {
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return outFormat.format(date);
	}
	
	public static Date str2Date(String dt, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = df.parse(dt);
			return date;
		} catch (ParseException e) {
			return date;
		}
	}
	public static int getYear() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.DAY_OF_MONTH);
	}
	public static int getHour() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.HOUR_OF_DAY);
	}
	public static int getYear(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int getHour(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.SECOND);
	}

	public static int getYear(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.SECOND);
	}
	public static Date addDays(Date date,int days) {
		return org.apache.commons.lang.time.DateUtils.addDays(date, days);
	}
	public static Date addYears(Date date,int years) {
		return org.apache.commons.lang.time.DateUtils.addYears(date, years);
	}
	public static Date addMonths(Date date,int months) {
		return org.apache.commons.lang.time.DateUtils.addMonths(date, months);
	}
	
	public static boolean checkLeapyear(int year) {
		boolean isLeapyear = false;
		if (year % 4 == 0 && year % 100 != 0) {
			isLeapyear = true;
		}
		if (year % 400 == 0)
			isLeapyear = true;
		else if (year % 4 != 0) {
			isLeapyear = false;
		}
		return isLeapyear;
	}
	
	
	public static int getDaysOfmonth(int month, int year) {
		
		int Dates = 0;
		if (month < 0 || month > 12) {
			System.out.println("month Error");
		}
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			Dates = 31;
		}
		if (month == 2 && checkLeapyear(year)) {
			Dates = 29;
		}
		if (month == 2 && !checkLeapyear(year)) {
			Dates = 28;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			Dates = 30;
		}
		return Dates;
	}
}
